<?PHP
  class MenuEntry{
    public $id    = -1;
    public $type  = 0;
	public $href  = '';
	public $menu  = '';
	
	public function display($globalstart,$globalend, $elementstart,$elementend,$class,$index){
      echo $this->getCode($globalstart,$globalend, $elementstart,$elementend,$class,$index);
	}
	
	public function getCode($globalstart,$globalend, $elementstart,$elementend,$class,$index){
	    if($this->type == 0){
		  $id = $GLOBALS['db']->EscapeString($this->href);
		  $link = $GLOBALS['db']->ReadField("SELECT alias FROM {'dbprefix'}pages WHERE id = '".$id."'");
          $res = $elementstart."<a href=\"".$link.".html\" title=\"".$this->title."\" 
                class=\"".$class." menue-".$id."-".$index."\">".$this->title."</a>".$elementend;
		}
		else if($this->type == 1){
		  $res = $elementstart."<a href=\"".$this->href."\" title=\"".$this->title."\" 
                 class=\"".$class." menue-".$id."-".$index."\">".$this->title."</a>".$elementend;
		}
		else{
		  $res = Menu::getCode($this->href,$globalstart,$globalend, $elementstart,$elementend,$class);
		}
		return $res;
	}
	
	public function getEditableCode(){
	  $template = new Template();
	  $template->load("menuentry_editable");
	  $template->assign_var("ID", $this->id);
	  $template->assign_var("TITLE",$this->title);
	  if($this->type == 0){
	    $template->assign_var("SELECTEDINTERNAL"," selected=\"1\"");
        $selector = new PageSelector();
	    $selector->name  = $this->id.'_href';
	    $selector->value = $this->href;
	    $template->assign_var("VALUECONTROL",$selector->getCode());
	  }
	  else{
	    $template->assign_var("SELECTEDINTERNAL","");
	    if($this->type == 1){
	      $template->assign_var("SELECTEDIEXTERNAL"," selected=\"1\"");
            
	      $template->assign_var("VALUECONTROL","<input name=\"".$this->id."_href\" value=\"".$this->href."\" />");
	    }
	    else{
	      $template->assign_var("SELECTEDIEXTERNAL","");
	      if($this->type == 2){
	        $template->assign_var("SELECTEDSUBMENU"," selected=\"1\"");
            $selector = new MenueSelector();
	        $selector->name  = $this->id.'_href';
	        $selector->value = $this->href;
	        $template->assign_var("VALUECONTROL",$selector->getCode());
	      }
	    }
	  }
	  return $template->getCode();
    }
	
	public function displayEditAble(){
      echo $this->getEdiableCode();
	}
	
	public function save(){
	  if($this->id == -1){
	    return $this->insert();
	  }
	  else{
	    return $this->update();
	  }
	}
	
	private function insert(){
	  $menu  = $GLOBALS['db']->EscapeString($this->menu);
	  $title = $GLOBALS['db']->EscapeString($this->title);
	  $href  = $GLOBALS['db']->EscapeString($this->href);
	  $type  = $GLOBALS['db']->EscapeString($this->type);
	  $maxID = $GLOBALS['db']->ReadField("SELECT MAX(id) FROM {'dbprefix'}menu 
                                          WHERE menuID = '".$menu."'");
	  $id = $maxID + 1;
      $res = $GLOBALS['db']->Execute("INSERT INTO {'dbprefix'}menu (id,menuID,title,href,type)
                                      VALUES('".$id."','".$menu."',
                                      '".$title."','".$href."','".$type."')");
      if($res){
        $args['menu']  = $menu;
	    $args['title'] = $title;
	    $args['href']  = $href;
	    EventManager::raiseEvent("menu_entry_added","../",$args);
	  }
	  return $res;
	}
	
	private function update(){
	  $id    = $GLOBALS['db']->EscapeString($this->id);
	  $menu  = $GLOBALS['db']->EscapeString($this->menu);
	  $title = $GLOBALS['db']->EscapeString($this->title);
	  $href  = $GLOBALS['db']->EscapeString($this->href);
	  $type  = $GLOBALS['db']->EscapeString($this->type);
      $res =  $GLOBALS['db']->Execute("UPDATE {'dbprefix'}menu SET 
	                                   href = '".$href."', 
		  			                   title = '".$title."', 
		  			                   type = '".$type."' 
						               WHERE id = '".$id."' 
						               AND menuID = '".$menu."'");
    if($res){
      $args['menu']  = $menu;
	  $args['title'] = $title;
	  $args['href']  = $href;
	  $args['id']    = $id;
	  EventManager::raiseEvent("menu_entry_edit","../",$args);
	}
	return $res;
	}
  }

?>